﻿using System;
using System.Linq.Dynamic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Buffalo.Lib.Common;
using Ext.Net.MVC;
using Ext.Net;
using System.Web.Mvc;
using Buffalo.SMECommon.DAL.Models;
using System.Data.Linq;
using System.Collections;
using System.Linq.Expressions;

namespace Buffalo.SMECommon.DAL
{
    public class ProductDAL : BaseDAL
    {
        public Product GetProduct(string IDProduct)
        {
            return GetEntity<Product>(IDProduct);
        }

        public List<Product> GetListProduct(int limit, int start, string dir, string sort, string criteria, string searchText, out int total)
        {
            return GetListEntity<Product>("gc_Product", string.Empty, limit, start, dir, sort, criteria, searchText, out total);
        }
        public bool SaveProducts(string jsonData)
        {
            return SaveEntities<Product>(jsonData, new string[] { "Code", "Name" });
        }
        public  bool LockProductIn(int monthNo, Product product, double quantity, double productInCost)
        {
            try
            {
                //accumulate Qty
                string productInQtyFieldName = string.Format("ProductInQty{0}", monthNo);
                double curProductInQty = Util.ToSafeDouble(Util.GetValue(product, productInQtyFieldName));
                Util.SetValue(product, productInQtyFieldName, curProductInQty + quantity);

                //OpenBalQty * OpenBalCost + ProductInQty * ProductInCost 
                string productInCostFieldName = string.Format("ProductInCost{0}", monthNo);
                Util.SetValue(product, productInCostFieldName, productInCost);

                //update
                return SaveEntity<Product>(product);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public  bool LockProductOut(int monthNo, Product product, double quantity)
        {
            try
            {
                //accumulate Qty
                string productOutQtyFieldName = string.Format("ProductOutQty{0}", monthNo);
                double curProductOutQty = Util.ToSafeDouble(Util.GetValue(product, productOutQtyFieldName));
                Util.SetValue(product, productOutQtyFieldName, curProductOutQty + quantity);

                //update
                return SaveEntity<Product>(product);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public  bool LockBookForYear(int curYear)
        {
            int total = 0;
            List<Product> listProduct = new ProductDAL().GetListProduct(0, 0, string.Empty, string.Empty, string.Empty, string.Empty, out total);
            foreach (Product product in listProduct)
            {
                LockBookForYear(curYear, product);
            }

            return true;
        }
        public  bool LockBookForYear(int curYear, Product product)
        {
            try
            {
                //update EndBalQty12, EndBalCost12
                double openBalQty12 = Util.ToSafeDouble(Util.GetValue(product, "OpenBalQty12"));
                double openBalCost12 = Util.ToSafeDouble(Util.GetValue(product, "OpenBalCost12"));

                double productInCost12 = Util.ToSafeDouble(Util.GetValue(product, "ProductInCost12"));
                double productInQty12 = Util.ToSafeDouble(Util.GetValue(product, "ProductInQty12"));
                double productOutCost12 = Util.ToSafeDouble(Util.GetValue(product, "ProductOutCost12"));
                double productOutQty12 = Util.ToSafeDouble(Util.GetValue(product, "ProductOutQty12"));

                double endBalQty12 = openBalQty12 + productInCost12 - productOutCost12;
                double endBalCost12 = openBalCost12 + productInCost12 - productOutCost12;

                Util.SetValue(product, "EndBalQty12", endBalQty12);
                Util.SetValue(product, "EndBalCost12", endBalCost12);

                return SaveEntity<Product>(product);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public  bool LockBookForMonth(int curMonth, int curYear)
        {
            if (curMonth == 12)
                LockBookForYear(curYear);

            int total = 0;
            List<Product> listProduct = new ProductDAL().GetListProduct(0, 0, string.Empty, string.Empty, string.Empty, string.Empty, out total);
            foreach (Product product in listProduct)
            {
                LockBookForMonth(curMonth, curYear, product);
            }

            return true;
        }
        public  bool LockBookForMonth(int curMonth, int curYear, Product product)
        {
            try
            {
                int nextMonth = curMonth + 1;

                string openBalQtyFieldName = string.Format("OpenBalQty{0}", curMonth);
                string openBalCostFieldName = string.Format("OpenBalCost{0}", curMonth);

                string productInQtyFieldName = string.Format("ProductInQty{0}", curMonth);
                string productInCostFieldName = string.Format("ProductInCost{0}", curMonth);
                string productOutQtyFieldName = string.Format("ProductOutQty{0}", curMonth);
                string productOutCostFieldName = string.Format("ProductOutCost{0}", curMonth);

                string nextOpenBalQtyFieldName = string.Format("OpenBalQty{0}", nextMonth);
                string nextOpenBalCostFieldName = string.Format("OpenBalCost{0}", nextMonth);

                double openBalQty = Util.ToSafeDouble(Util.GetValue(product, openBalQtyFieldName));
                double openBalCost = Util.ToSafeDouble(Util.GetValue(product, openBalCostFieldName));
                double productInQty = Util.ToSafeDouble(Util.GetValue(product, productInQtyFieldName));
                double productInCost = Util.ToSafeDouble(Util.GetValue(product, productInCostFieldName));
                double productOutQty = Util.ToSafeDouble(Util.GetValue(product, productOutQtyFieldName));
                double productOutCost = Util.ToSafeDouble(Util.GetValue(product, productOutCostFieldName));

                double nextOpenBalQty = openBalQty + productInQty - productOutQty;
                double nextOpenBalCost = openBalCost + productInCost - productOutCost;

                Util.SetValue(product, openBalQtyFieldName, nextOpenBalQty);
                Util.SetValue(product, openBalCostFieldName, nextOpenBalCost);

                return SaveEntity<Product>(product);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool UnLockProductIn(int monthNo, Product product, double quantity, double productInCost)
        {
            try
            {
                string productInQtyFieldName = string.Format("ProductInQty{0}", monthNo);
                double curProductInQty = Util.ToSafeDouble(Util.GetValue(product, productInQtyFieldName));
                double deAccumulatedQty = curProductInQty - quantity;
                Util.SetValue(product, productInQtyFieldName, deAccumulatedQty);

                string productInCostFieldName = string.Format("ProductInCost{0}", monthNo);
                Util.SetValue(product, productInCostFieldName, productInCost);
                //update
                return SaveEntity<Product>(product);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
